package com.situ.blog.controller;

import com.situ.blog.pojo.enity.Blog;
import com.situ.blog.pojo.query.BlogQuery;
import com.situ.blog.service.IBlogService;
import com.situ.blog.service.Impl.BlogServiceImpl;
import com.situ.blog.util.MyBeanUtils;
import com.situ.blog.util.Result;
import org.springframework.util.ObjectUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/blog")
public class BlogController extends BaseController{
    private IBlogService blogService= new BlogServiceImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        switch (method) {
            case "selectByType":
                selectByType(req, resp);
                break;
            case "selectById":
                selectById(req, resp);
                break;
        }
    }

    private void selectByType(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("BlogController.selectByType");
        //String typeIdStr = req.getParameter("typeId");
        //Integer typeId = ObjectUtils.isEmpty(typeIdStr) ? null : Integer.parseInt(typeIdStr);
        BlogQuery blogQuery = MyBeanUtils.copyToBean(req, BlogQuery.class);
        List<Blog> list = blogService.selectByType(blogQuery);
        toJSON(resp, Result.ok(list));
    }

    private void selectById(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("BlogController.selectById");
        String id = req.getParameter("id");
        Blog blog = blogService.selectById(Integer.parseInt(id));
        toJSON(resp, Result.ok(blog));
    }
}
